package com.vvv.designpattern.bridge.function;

import com.alibaba.fastjson.JSONObject;
import com.vvv.designpattern.bridge.abst.factory.RegisterLoginComponentFactory;
import com.vvv.designpattern.pojo.UserInfo;
import com.vvv.designpattern.repo.UserRepository;
import com.vvv.designpattern.uilts.HttpClientUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author vvv
 * @date 2023-11-16 14 18
 * discription 右路的具体实现类，gitee第三方登录方案
 */
@Component
public class RegisterLoginByGitee
        extends AbstractRegisterLoginFunc implements RegisterLoginFuncInterface {

    @Value("${gitee.state}")
    private String giteeState;
    @Value("${gitee.token.url}")
    private String giteeTokenUrl;
    @Value("${gitee.user.url}")
    private String giteeUserUrl;
    @Value("${gitee.user.prefix}")
    private String giteeUserPrefix;

    @Resource
    private UserRepository userRepository;

    /**
     * PostConstruct注解可以在完成bean的注入后，立即调用标志为@PostConstruct的注解的方法
     */
    @PostConstruct
    public void initFuncMap() {
        RegisterLoginComponentFactory.funcMap.put("GITEE", this);
    }

    @Override
    public String login3rd(HttpServletRequest request) {
        String state = request.getParameter("state");
        String code = request.getParameter("code");
        // 1. 验证一下state值是否和后端的state值相同
        if (!giteeState.equals(state)) {
            throw new UnsupportedOperationException("Invaild state!");
        }
        // 2. 根据code请求token
        String tokenUrl = giteeTokenUrl.concat(code);
        JSONObject tokenResponse = HttpClientUtils.execute(tokenUrl, HttpMethod.POST);
        if (tokenResponse.get("error") != null) {
            throw new UnsupportedOperationException("Invaild state!");
        }


        String token = String.valueOf(tokenResponse.get("access_token"));
        // 3. 根据token请求用户信息、
        String userUrl = giteeUserUrl.concat(token);
        JSONObject userInfoResponse = HttpClientUtils.execute(userUrl, HttpMethod.GET);
        // 4. 设置用户名和密码
        String username = giteeUserPrefix.concat(String.valueOf(userInfoResponse.get("name")));
        String password = username;
        // 4. 登录和注册
        return this.autoRegister3rdAndLogin(username, password);
    }

    private String autoRegister3rdAndLogin(String username, String password) {
        // 1. 检查用户是否已经存在
        if (super.commonCheckUserExists(username, userRepository)) {
            // 1.1 存在直接登录
            return super.commonLogin(username, password, userRepository);
        }
        // 2. 不存在创建UserInfo，先注册，在登录
        UserInfo userInfo = new UserInfo();
        userInfo.setUserName(username);
        userInfo.setUserPassword(password);
        super.commonRegister(userInfo, userRepository);

        return super.commonLogin(username, password, userRepository);
    }
}
